/*
 * Spinneret - Semi-Structured Database Engine
 * Copyright (c) 2009 Tom Bradford <spinneretdb.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <string.h>
#include <errno.h>
#include <fuse.h>

#include "main/fusefs.h"

/* Private Types and Variables ***********************************************/

typedef struct fuse_conn_info fuse_conn_info_t;
typedef struct fuse_file_info fuse_file_info_t;
typedef struct fuse_operations fuse_operations_t;
typedef struct stat stat_t;
typedef struct statvfs statvfs_t;
typedef struct timespec timespec_t;

/** Implementation type for FuseFS instances */
struct sdb_fusefs_t {
    void* placeholder;  
};

/* Private Functions *********************************************************/

static int private_getattr(const char* path, stat_t* buf)
{
    memset(buf, 0, sizeof(stat_t));

    if (strcmp(path, "/") == 0) {
        /* The root directory of our file system. */
        buf->st_mode = S_IFDIR | 0755;
        buf->st_nlink = 3;
        return 0;
    }
    return -ENOENT;
}

/*---------------------------------------------------------------------------*/

static int private_readlink(const char* path, char* buf, size_t size)
{
    return -ENOENT;
}

/*---------------------------------------------------------------------------*/

static int private_mknod(const char* path, mode_t mode, dev_t rdev)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_mkdir(const char* path, mode_t mode)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_unlink(const char* path)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_rmdir(const char* path)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_symlink(const char* from, const char* to)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_rename(const char* from, const char* to)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_link(const char* from, const char* to)
{
    return -ENOSYS;
}

static int private_chmod(const char* path, mode_t mode)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_chown(const char* path, uid_t uid, gid_t gid)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_truncate(const char* path, off_t size)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_open(const char* path, fuse_file_info_t* fi)
{
    return -ENOENT;
}

/*---------------------------------------------------------------------------*/

static int private_read(const char* path, char* buf, size_t size, off_t off,
                        fuse_file_info_t* fi)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_write(const char* path, const char* buf, size_t size,
                         off_t off, fuse_file_info_t* fi)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_statfs(const char* path, statvfs_t* stbuf)
{
    memset(stbuf, 0, sizeof(*stbuf));
    stbuf->f_files = 2;  /* For . and .. in the root */
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_flush(const char* path, fuse_file_info_t* fi)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_release(const char* path, fuse_file_info_t* fi)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_fsync(const char* path, int datasync, fuse_file_info_t* fi)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_setxattr(const char* path, const char* name,
                            const char* value, size_t size, int flags,
                            uint32_t position)
{
    return -ENOTSUP;
}

/*---------------------------------------------------------------------------*/

static int private_getxattr(const char* path, const char* name, char* value,
                            size_t size, uint32_t position)
{
    return -ENOATTR;
}

/*---------------------------------------------------------------------------*/

static int private_listxattr(const char* path, char* list, size_t size)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_removexattr(const char* path, const char* name)
{
    return -ENOATTR;
}

/*---------------------------------------------------------------------------*/

static int private_readdir(const char* path, void* buf,
                           fuse_fill_dir_t filler, off_t off,
                           fuse_file_info_t* fi)
{

    if (strcmp(path, "/") != 0) /* We only recognize the root directory. */
        return -ENOENT;

    filler(buf, ".", NULL, 0);           /* Current directory (.)  */
    filler(buf, "..", NULL, 0);          /* Parent directory (..)  */

    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_releasedir(const char* path, fuse_file_info_t* fi)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_fsyncdir(const char* path, int datasync,
                            fuse_file_info_t* fi)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static sdb_fusefs_t* private_init(fuse_conn_info_t* conn)
{
    sdb_mem_t* mem = sdb_mem_new(NULL);
    sdb_fusefs_t* fusefs = sdb_fusefs_new(mem);
    sdb_fusefs_init(fusefs);

    return fusefs;
}

/*---------------------------------------------------------------------------*/

static void private_destroy(sdb_fusefs_t* fusefs)
{
    sdb_ptr_free(fusefs);
}

/*---------------------------------------------------------------------------*/

static int private_access(const char* path, int mask)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_create(const char* path, mode_t mode, fuse_file_info_t* fi)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_ftruncate(const char* path, off_t size,
                             fuse_file_info_t* fi)
{
    return -ENOSYS;
}

/*---------------------------------------------------------------------------*/

static int private_fgetattr(const char* path, stat_t* buf,
                            fuse_file_info_t *fi)
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static int private_utimens(const char* path, const timespec_t tv[2])
{
    return 0;
}

/*---------------------------------------------------------------------------*/

static void private_finalizer(sdb_fusefs_t* fusefs)
{

}

/* Public Functions **********************************************************/

sdb_fusefs_t* sdb_fusefs_new(sdb_mem_t* mem)
{
    return sdb_mem_malloc(mem, sizeof(sdb_fusefs_t));
}

/*---------------------------------------------------------------------------*/

sdb_fusefs_t* sdb_fusefs_init(sdb_fusefs_t* fusefs)
{
    sdb_ptr_setDisposalFunction(fusefs, private_finalizer);

    return fusefs;
}

/*---------------------------------------------------------------------------*/

int sdb_fusefs_main(int argc, char* argv[])
{
    static fuse_operations_t ops = {
      .getattr     = private_getattr,
      .readlink    = private_readlink,
      .mknod       = private_mknod,
      .mkdir       = private_mkdir,
      .unlink      = private_unlink,
      .rmdir       = private_rmdir,
      .symlink     = private_symlink,
      .rename      = private_rename,
      .link        = private_link,
      .chmod       = private_chmod,
      .chown       = private_chown,
      .truncate    = private_truncate,
      .open        = private_open,          // Special
      .read        = private_read,
      .write       = private_write,
      .statfs      = private_statfs,
      .flush       = private_flush,         // Special
      .release     = private_release,       // Special
      .fsync       = private_fsync,
      .setxattr    = private_setxattr,
      .getxattr    = private_getxattr,
      .listxattr   = private_listxattr,
      .removexattr = private_removexattr,
      .readdir     = private_readdir,
      .releasedir  = private_releasedir,
      .fsyncdir    = private_fsyncdir,      // Special
      .init        = private_init,          // Special
      .destroy     = private_destroy,       // Special
      .access      = private_access,        // Special
      .create      = private_create,        // Special
      .ftruncate   = private_ftruncate,     // Special
      .fgetattr    = private_fgetattr,      // Special
      // .lock        = private_lock        // Special
      .utimens     = private_utimens
      // .bmap        = private_bmap
    };

    return fuse_main(argc, argv, &ops, NULL);
}
